<%@page import="edu.gatech.team404.tool.StatusCode"%>
<%@page import="java.util.ArrayList"%>
<%@page import="edu.gatech.team404.Team"%>
<%@page import="edu.gatech.team404.Lab"%>
<%@page import="edu.gatech.team404.Course"%>
<%@page import="edu.gatech.team404.TeamAssignment"%>
<%@page import="edu.gatech.team404.dal.CourseDAL"%>
<%@page import="edu.gatech.team404.dal.TeamDAL"%>
<%@page import="edu.gatech.team404.dal.LabDAL"%>
<%@page import="edu.gatech.team404.Account"%>
<%@page import="edu.gatech.team404.dal.AccountDAL"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>

<%@include file="header.jsp" %>

<div id="content">
    <%@include file="sidebar.jsp" %>
    
    <%@include file="toolbar.jsp" %>
    
    <div id="main">
    	<h1 align="center">Team</h1>
    	
    	<div id="create">
	    	<div class="status">
		    	<%
	    			Boolean status = (Boolean) session.getAttribute("createTeamStatus");
	    			if (status != null) {
		    			if (status) {
		    				out.println("<p style=\"color:blue;\">");
							out.println("Team has been successfully created.");
							out.println("</p>");
		    			}
		    			else {
		    				out.println("<p style=\"color:red;\">");
							out.println("Cannot create a Team. Please try again later.");
							out.println("</p>");
		    			}
	    			}
	    		%>
	    	</div>
	    	
	    	<div class="viewTable">
		    	<form action="CreateTeam" method="post">
		    		<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    		<table cellpadding="10">
		    			<tr>
		    				<th align="right">Team Name</th>
		    				<td align="left"><input type="text" name="name" value="" size="50" maxlength="15"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Lab</th>
			    			<td align="left">
				    			<select name="labID">
									<option value="">Select lab</option>
									<%
									final String courseName = request.getParameter("course");
									final LabDAL lDal = new LabDAL();
									final TeamDAL tDal = new TeamDAL();
							    	
									final List<Lab>labs = lDal.getAllLabsInCourse(courseName);
							    	
							    	for(Lab lab : labs) {
							    		if (lab.getTeamAssignment() == TeamAssignment.SELF_SELECT) {
							    			out.print("<option value=\"" + lab.getId() + "\">" + lab.getName() + "</option>");
							    		}
							    	}
							    	
							    	lDal.close();
									%>
								</select>
							</td>
		    			</tr>
		    			<tr>
		    				<td colspan="2" align="center">
		    					<input type="submit" value="Create Team!" />
		    				</td>
		    			</tr>
		    		</table>
	    		</form>
	    	</div>
    	</div>
    	
    	<div id="review">
    	<table class="viewTable" border="1" cellpadding="10">
    		<tr>
    			<th>Team Name</th>
    			<th>Members</th>
    		</tr>
    		<% 
    		
    		for (Lab lab : labs) {
    			out.println ("<tr>");
    			out.println ("<td colspan=2 bgcolor=\"#F6F6CC\">");
    			out.println ("** " + lab.getName() + " **");
    			out.println ("</td>");
    			out.println ("</tr>");
	
    			List<Team> teams = tDal.getTeamsInLab(lab.getId());
    			for (Team team : teams) {
    				out.println ("<tr>");
    				
    				out.println ("<td>");
					out.println (team.getName());
        			out.println ("</td>");
        			
        			out.println ("<td>");
        			List<Account> members = team.getMembers();
        			for (Account member : members) {
        				if (member != null) {
        					out.println(member.getName() + "<br/>");
        				}
        			}
        			out.println ("</td>");
        			
        			out.println ("</tr>");
    			}
    			
    			
    		}
	    	tDal.close();
	    	%>
	    	</table>
    	</div>
    	
    	<div id="update">
    		<h2 style="font-size:medium;">Join Team</h2>
    		<div class="status">
    			<%
	    			Boolean joinStatus = (Boolean) session.getAttribute("joinTeamStatus");
	    			if (joinStatus != null) {
		    			if (joinStatus) {
		    				out.println("<p style=\"color:blue;\">");
							out.println("Team has been successfully joined.");
							out.println("</p>");
		    			}
		    			else {
		    				out.println("<p style=\"color:red;\">");
							out.println("Cannot join this Team.");
							out.println("</p>");
		    			}
	    			}
	    		%>
	    	</div>
	    	
	    	<div class="viewTable">
	    	<form action="JoinTeam" method="post">
	    		<table cellpadding="10">
	    			<tr>
	    				<td align="left">
	    					<input type="hidden" name="username" value='<%=getCookieByName(request.getCookies(), "username").getValue() %>'/>
	    				</td>
	    				<td>
		    				<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    			</td>
	    			</tr>
	    			<tr>
	    				<th align="right">Team</th>
		    			<td align="left">
			    			<select name="teamID">
			    				<option value="">Select team</option>								
								<%
								for (Lab lab : labs) {
									if (lab.getTeamAssignment() == TeamAssignment.SELF_SELECT) {
										out.println ("<optgroup label=\"** " + lab.getName() + " **\">");
										
										List<Team> teams = tDal.getTeamsInLab(lab.getId());
										for (Team team : teams) {
											out.print("<option value=\"" + team.getTeamID() + "\">" + team.getName() + "</option>");
										}
										
										out.println ("</optgroup");
									}
								}
								tDal.close();
								%>
							</select>
						</td>
	    			</tr>
	    			<tr>
	    				<td colspan="2" align="center">
	    					<input type="submit" value="Join Team!" />
	    				</td>
	    			</tr>
	    		</table>
	    	</form>
	    	</div>
	    	
	    	<h2 style="font-size:medium;">Remove from Team</h2>
    		<div class="status">
    			<%
	    			Boolean disjoinStatus = (Boolean) session.getAttribute("disjoinTeamStatus");
	    			if (disjoinStatus != null) {
		    			if (disjoinStatus) {
		    				out.println("<p style=\"color:blue;\">");
							out.println("You are not part of the Team anymore.");
							out.println("</p>");
		    			}
		    			else {
		    				out.println("<p style=\"color:red;\">");
							out.println("Cannot remove you account from this Team.");
							out.println("</p>");
		    			}
	    			}
	    		%>
	    	</div>
	    	
	    	<div class="viewTable">
	    	<form action="DisjoinTeam" method="post">
	    		<table cellpadding="10">
	    			<tr>
	    				<td align="left">
	    					<input type="hidden" name="username" value='<%=getCookieByName(request.getCookies(), "username").getValue() %>'/>
	    				</td>
	    				<td>
		    				<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    			</td>
	    			</tr>
	    			<tr>
	    				<th align="right">Team</th>
		    			<td align="left">
			    			<select name="teamID">
			    				<option value="">Select team</option>								
								<%
								for (Lab lab : labs) {
									if (lab.getTeamAssignment() == TeamAssignment.SELF_SELECT) {
										out.println ("<optgroup label=\"** " + lab.getName() + " **\">");
										
										List<Team> teams = tDal.getTeamsInLab(lab.getId());
										for (Team team : teams) {
											out.print("<option value=\"" + team.getTeamID() + "\">" + team.getName() + "</option>");
										}
										
										out.println ("</optgroup");
									}
								}
								tDal.close();
								%>
							</select>
						</td>
	    			</tr>
	    			<tr>
	    				<td colspan="2" align="center">
	    					<input type="submit" value="Remove from Team!" />
	    				</td>
	    			</tr>
	    		</table>
	    	</form>
	    	</div>
	    	
    	</div>
    	
    	<div id="delete">
    		<div class="status">
    			<%
	    			status = (Boolean) session.getAttribute("teamDeleteStatus");
	    			if (status != null) {
		    			if (status) {
		    				out.println("<p style=\"color:blue;\">");
							out.println("Team has been successfully deleted.");
							out.println("</p>");
		    			}
		    			else {
		    				out.println("<p style=\"color:red;\">");
							out.println("Cannot delete this Team.");
							out.println("</p>");
		    			}
	    			}
	    		%>
	    	</div>
	    	
	    	<div class="viewTable">
	    	<form action="DeleteTeam" method="post">
	    		<table cellpadding="10">
	    			<tr>
	    				<td align="left">
	    					<input type="hidden" name="username" value='<%=getCookieByName(request.getCookies(), "username").getValue() %>'/>
	    				</td>
	    				<td>
		    				<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    			</td>
	    			</tr>
	    			<tr>
	    				<th align="right">Team</th>
		    			<td align="left">
			    			<select name="teamID">
								<option value="">Select team</option>	
								<%
								for (Lab lab : labs) {
									if (lab.getTeamAssignment() == TeamAssignment.SELF_SELECT) {
										out.println ("<optgroup label=\"** " + lab.getName() + " **\">");
										
										List<Team> teams = tDal.getTeamsInLab(lab.getId());
										for (Team team : teams) {
											out.print("<option value=\"" + team.getTeamID() + "\">" + team.getName() + "</option>");
										}
										
										out.println ("</optgroup");
									}
								}
								tDal.close();
								%>
							</select>
						</td>
	    			</tr>
	    			<tr>
	    				<td colspan="2" align="center">
	    					<input type="submit" value="Delete Team!" />
	    				</td>
	    			</tr>
	    		</table>
	    	</form>
    	</div>
    	
    	<script type="text/javascript"> 
			Script404.initShow("<%= session.getAttribute("feed") %>"); 	   
		</script> 
    </div>
</div>

<%@include file="footer.jsp" %>